import { app as n, ipcMain as l, BrowserWindow as m } from "electron";
import t from "path";
import { fileURLToPath as f } from "url";
n.commandLine.appendSwitch("disable-gpu-process-crash-limit");
n.commandLine.appendSwitch("ignore-gpu-blocklist");
n.commandLine.appendSwitch("enable-gpu-rasterization");
n.commandLine.appendSwitch("enable-zero-copy");
n.commandLine.appendSwitch("enable-webgl");
n.commandLine.appendSwitch("enable-accelerated-2d-canvas");
n.disableHardwareAcceleration = !1;
const p = f(import.meta.url), a = t.dirname(p);
let e;
async function r() {
  if (e = new m({
    width: 1400,
    height: 900,
    resizable: !0,
    minWidth: 800,
    minHeight: 600,
    backgroundColor: "#212334",
    // 设置窗口背景颜色
    icon: t.join(a, "../public/icon.ico"),
    // 设置应用图标
    titleBarStyle: process.platform === "darwin" ? "hiddenInset" : "default",
    // macOS使用隐藏式标题栏
    titleBarOverlay: process.platform === "win32" ? {
      color: "#11142C",
      // Windows标题栏背景颜色
      symbolColor: "#FFFFFF"
      // Windows标题栏按钮颜色
    } : !1,
    // 只在Windows上启用标题栏覆盖
    frame: !0,
    // 显示窗口边框
    autoHideMenuBar: !0,
    // 自动隐藏菜单栏（Windows上）
    webPreferences: {
      // 禁用安全沙箱
      sandbox: !1,
      // 允许加载远程资源
      webSecurity: !1,
      nodeIntegration: !0,
      contextIsolation: !0,
      preload: t.join(a, "preload.cjs"),
      // 添加媒体访问权限
      allowRunningInsecureContent: !1,
      enableWebSQL: !1,
      enableRemoteModule: !1,
      // 启用WebGL和硬件加速
      webgl: !0,
      experimentalFeatures: !0,
      // 启用摄像头和麦克风访问
      permissions: {
        media: !0
      }
    }
  }), process.platform === "win32")
    try {
      e.setBackgroundColor("#11142C"), e.setMenuBarVisibility(!1);
    } catch (i) {
      console.error("设置Windows窗口样式时出错:", i);
    }
  if (process.env.NODE_ENV === "development")
    e.loadURL("http://localhost:5173"), e.webContents.openDevTools();
  else {
    console.log("Production mode - loading index.html"), console.log("__dirname:", a), console.log("process.resourcesPath:", process.resourcesPath), console.log("process.cwd():", process.cwd());
    const i = [
      // 开发时的路径
      t.join(a, "../dist/index.html"),
      // 打包后可能的路径
      t.join(process.resourcesPath, "dist/index.html"),
      t.join(process.resourcesPath, "app/dist/index.html"),
      // ASAR 包内的路径
      t.join(a, "../dist/index.html"),
      // 备用路径
      t.join(process.cwd(), "dist/index.html")
    ];
    let o = !1;
    for (const s of i)
      try {
        console.log("Trying to load from:", s), await e.loadFile(s), console.log("✓ Successfully loaded from:", s), o = !0;
        break;
      } catch (d) {
        console.log("✗ Failed to load from:", s, "-", d.message);
      }
    o || (console.error("❌ Failed to load index.html from any path"), e.loadURL("data:text/html,<h1>应用加载失败</h1><p>无法找到应用文件，请重新安装应用。</p>"));
  }
  e.webContents.on("before-input-event", (i, o) => {
    (o.key === "F12" || o.control && o.shift && o.key === "I") && (e.webContents.isDevToolsOpened() ? e.webContents.closeDevTools() : e.webContents.openDevTools()), (o.control && o.key === "r" || o.key === "F5") && e.webContents.reload();
  }), e.webContents.session.setPermissionRequestHandler((i, o, s) => {
    if (o === "media" || o === "microphone" || o === "camera" || o === "mediaKeySystem" || o === "fullscreen") {
      s(!0);
      return;
    }
    s(!1);
  }), e.on("closed", function() {
    e = null;
  });
}
n.whenReady().then(r);
n.on("window-all-closed", function() {
  process.platform !== "darwin" && n.quit();
});
n.on("activate", function() {
  e === null && r();
});
l.on("message-from-renderer", (c, i) => {
  console.log("收到渲染进程消息:", i), setTimeout(() => {
    e && !e.isDestroyed() && e.webContents.send("message-from-main", "这是主进程的回复");
  }, 1e3);
});
l.on("window-minimize", () => {
  e && e.minimize();
});
l.on("window-maximize", () => {
  e && (e.isMaximized() ? e.unmaximize() : e.maximize());
});
l.on("window-close", () => {
  e && e.close();
});
